package com.yxch.admin.service.impl.user;


import com.yxch.admin.mapper.user.UserMapper;
import com.yxch.admin.model.commen.PageResult;
import com.yxch.admin.model.user.UserDTO;
import com.yxch.admin.model.user.UserLoginDTO;
import com.yxch.admin.model.user.UserMute;
import com.yxch.admin.model.user.UserVO;
import com.yxch.admin.service.user.UserService;
import com.yxch.admin.utils.ExceptionEnum;
import com.yxch.admin.utils.JwtUtil;
import com.yxch.admin.utils.ResultAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Transactional(rollbackFor = Exception.class)
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    /**
     * 管理员登录
     *
     * @param userLoginDTO
     * @return
     */
    @Override
    public ResultAPI login(UserLoginDTO userLoginDTO) {
        UserVO userVO = userMapper.getUserByPhoneandPwd(userLoginDTO);
        if (userVO == null || !userVO.getUser_type().equals("admin")) {
            return ResultAPI.error(ExceptionEnum.LOGIN_ERROR.getResultCode(), ExceptionEnum.LOGIN_ERROR.getResultMsg());
        }
        //获取jwt
        String token = null;
        try {
            Map<String, Object> claims = new HashMap();
            claims.put("username", userVO.getUsername());
            claims.put("id", userVO.getId());
            claims.put("phone_num", userVO.getPhone_num());
            token = JwtUtil.generateJwt(claims);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        userVO.setToken(token);
        //刷新登录时间
        userMapper.addLoginlastime(userLoginDTO);
        return ResultAPI.success(userVO);
    }

    /**
     * 获取管理员信息
     *
     * @param token
     * @return
     */
    @Override
    public ResultAPI getUserInfo(String token) {
        Map<String, Object> claims = JwtUtil.parseJWT(token);
        Integer id = (Integer) claims.get("id");
        UserVO userVO = userMapper.getUserById(id);
        return ResultAPI.success(userVO);
    }

    /**
     * 获取学生信息列表
     *
     * @param userDTO
     * @return
     */
    @Override
    public ResultAPI getStuInfo(UserDTO userDTO) {
        Integer pageNum = userDTO.getPageNum();
        Integer pageSize = userDTO.getPageSize();
        Integer start = (pageNum - 1) * pageSize;
        userDTO.setStart(start);

        PageResult pageResult = userMapper.getStuInfos(userDTO);
        return ResultAPI.success(pageResult);
    }

    /**
     * 获取禁言信息
     *
     * @param userId
     * @return
     */
    @Override
    public ResultAPI getMuteInfo(Integer userId) {
        UserMute userMute = userMapper.getMuteInfo(userId);
        return ResultAPI.success(userMute);
    }

    @Override
    public ResultAPI AddandUpdateMuteUser(UserMute userMute) {
        if (userMute.getMute_id() != null) {
            userMapper.UpdateMuteUser(userMute);
        } else {
            userMapper.InsertMuteUser(userMute);
        }
        return ResultAPI.success();
    }

    @Override
    public ResultAPI getTechInfo(UserDTO userDTO) {
        Integer pageNum = userDTO.getPageNum();
        Integer pageSize = userDTO.getPageSize();
        Integer start = (pageNum - 1) * pageSize;
        userDTO.setStart(start);

        PageResult pageResult = userMapper.getTechInfos(userDTO);
        return ResultAPI.success(pageResult);
    }
}
